package com.xxkk.spring_boot_web.sdk.KBZ.utils;

import kotlin.UByte;

import java.io.ByteArrayOutputStream;

public class Base64Util {
    private static final char[] base64EncodeChars = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
    private static byte[] base64DecodeChars = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1};

    private Base64Util() {
    }

    /* JADX WARN: Code restructure failed: missing block: B:28:0x0057, code lost:
        if (r5 != (-1)) goto L24;
     */
    /* JADX WARN: Code restructure failed: missing block: B:30:0x005a, code lost:
        r1.write(((r4 & 15) << 4) | ((r5 & 60) >>> 2));
     */
    /* JADX WARN: Code restructure failed: missing block: B:31:0x0066, code lost:
        r4 = r2 + 1;
        r2 = r8[r2];
     */
    /* JADX WARN: Code restructure failed: missing block: B:32:0x006a, code lost:
        if (r2 != 61) goto L27;
     */
    /* JADX WARN: Code restructure failed: missing block: B:34:0x0070, code lost:
        return r1.toByteArray();
     */
    /* JADX WARN: Code restructure failed: missing block: B:35:0x0071, code lost:
        r2 = com.huawei.p120kbz.utils.encrypt.Base64Util.base64DecodeChars[r2];
     */
    /* JADX WARN: Code restructure failed: missing block: B:36:0x0075, code lost:
        if (r4 >= r0) goto L38;
     */
    /* JADX WARN: Code restructure failed: missing block: B:37:0x0077, code lost:
        if (r2 == (-1)) goto L30;
     */
    /* JADX WARN: Code restructure failed: missing block: B:39:0x007a, code lost:
        r2 = r4;
     */
    /* JADX WARN: Code restructure failed: missing block: B:40:0x007c, code lost:
        if (r2 != (-1)) goto L33;
     */
    /* JADX WARN: Code restructure failed: missing block: B:42:0x007f, code lost:
        r1.write(r2 | ((r5 & 3) << 6));
        r2 = r4;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static byte[] decode(String str) throws Exception {
        int i;
        byte b;
        int i2;
        byte b2;
        byte[] bytes = str.getBytes("UTF-8");
        int length = bytes.length;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(length);
        int i3 = 0;
        while (i3 < length) {
            while (true) {
                i = i3 + 1;
                b = base64DecodeChars[bytes[i3]];
                if (i >= length || b != -1) {
                    break;
                }
                i3 = i;
            }
            if (b == -1) {
                break;
            }
            while (true) {
                i2 = i + 1;
                b2 = base64DecodeChars[bytes[i]];
                if (i2 >= length || b2 != -1) {
                    break;
                }
                i = i2;
            }
            if (b2 == -1) {
                break;
            }
            byteArrayOutputStream.write((b << 2) | ((b2 & 48) >>> 4));
            while (true) {
                int i4 = i2 + 1;
                byte b3 = bytes[i2];
                if (b3 == 61) {
                    return byteArrayOutputStream.toByteArray();
                }
                byte b4 = base64DecodeChars[b3];
                if (i4 >= length || b4 != -1) {
                    break;
                }
                i2 = i4;
            }
        }
        return byteArrayOutputStream.toByteArray();
    }

    public static String encode(byte[] bArr) {
        StringBuffer stringBuffer = new StringBuffer();
        int length = bArr.length;
        int i = 0;
        while (true) {
            if (i >= length) {
                break;
            }
            int i2 = i + 1;
            byte b = bArr[i];
            int i3 = b & UByte.MAX_VALUE;
            if (i2 == length) {
                char[] cArr = base64EncodeChars;
                stringBuffer.append(cArr[i3 >>> 2]);
                stringBuffer.append(cArr[(b & 3) << 4]);
                stringBuffer.append("==");
                break;
            }
            int i4 = i + 2;
            byte b2 = bArr[i2];
            if (i4 == length) {
                char[] cArr2 = base64EncodeChars;
                stringBuffer.append(cArr2[i3 >>> 2]);
                stringBuffer.append(cArr2[((b & 3) << 4) | ((b2 & 240) >>> 4)]);
                stringBuffer.append(cArr2[(b2 & 15) << 2]);
                stringBuffer.append("=");
                break;
            }
            i += 3;
            byte b3 = bArr[i4];
            char[] cArr3 = base64EncodeChars;
            stringBuffer.append(cArr3[i3 >>> 2]);
            stringBuffer.append(cArr3[((b & 3) << 4) | ((b2 & 240) >>> 4)]);
            stringBuffer.append(cArr3[((b2 & 15) << 2) | ((b3 & 192) >>> 6)]);
            stringBuffer.append(cArr3[b3 & 63]);
        }
        return stringBuffer.toString();
    }
}
